﻿
echowithcolor(COLOR GREEN "-- Configure ${CMAKE_CURRENT_LIST_DIR}")

set(PROJECT_LIBATAPP_ROOT_SRC_DIR ${CMAKE_CURRENT_LIST_DIR})

list(APPEND PROJECT_LIBATAPP_PUBLIC_INCLUDE_DIRS ${PROJECT_LIBATAPP_ROOT_INC_DIR})
file(RELATIVE_PATH PROJECT_ROOT_RELINC_DIR ${CMAKE_CURRENT_LIST_DIR}
     ${PROJECT_LIBATAPP_ROOT_INC_DIR})

unset(PROJECT_LIB_SRC_LIST)
file(
  GLOB_RECURSE
  PROJECT_LIB_SRC_LIST
  ${PROJECT_ROOT_RELINC_DIR}/*.h
  ${PROJECT_ROOT_RELINC_DIR}/*.hpp
  ${PROJECT_ROOT_RELINC_DIR}/*.hxx
  ${PROJECT_LIBATAPP_ROOT_SRC_DIR}/*.h
  ${PROJECT_LIBATAPP_ROOT_SRC_DIR}/*.hpp
  ${PROJECT_LIBATAPP_ROOT_SRC_DIR}/*.c
  ${PROJECT_LIBATAPP_ROOT_SRC_DIR}/*.cpp
  ${PROJECT_LIBATAPP_ROOT_SRC_DIR}/*.cc
  ${PROJECT_LIBATAPP_ROOT_SRC_DIR}/*.cxx)
unset(PROJECT_LIBATAPP_SRC_LIST)
foreach(SRC_FILE ${PROJECT_LIB_SRC_LIST})
  get_filename_component(SRC_FILE_EXT ${SRC_FILE} EXT)
  if(NOT SRC_FILE_EXT STREQUAL ".pb.h" AND NOT SRC_FILE_EXT STREQUAL ".pb.cc")
    list(APPEND PROJECT_LIBATAPP_SRC_LIST ${SRC_FILE})
  endif()
endforeach()

source_group_by_dir(PROJECT_LIB_SRC_LIST)

set(PROJECT_LIBATAPP_PROTOCOL_SRC_LIST "${PROJECT_LIBATAPP_ROOT_INC_DIR}/atframe/atapp_conf.pb.h"
                                       "${PROJECT_LIBATAPP_ROOT_SRC_DIR}/atframe/atapp_conf.pb.cc")

# ============ libatapp - src ============
add_custom_command(
  OUTPUT ${PROJECT_LIBATAPP_PROTOCOL_SRC_LIST}
  COMMAND
    ${3RD_PARTY_PROTOBUF_BIN_PROTOC} "-I" ${PROJECT_LIBATAPP_ROOT_INC_DIR} "-I"
    ${3RD_PARTY_PROTOBUF_INC_DIR} "-I" "${LIBATBUS_ROOT}/include"
    "--cpp_out=${PROJECT_LIBATAPP_ROOT_INC_DIR}"
    "${PROJECT_LIBATAPP_ROOT_INC_DIR}/atframe/atapp_conf.proto"
  COMMAND ${CMAKE_COMMAND} -E rename "${PROJECT_LIBATAPP_ROOT_INC_DIR}/atframe/atapp_conf.pb.cc"
          "${PROJECT_LIBATAPP_ROOT_SRC_DIR}/atframe/atapp_conf.pb.cc"
  DEPENDS "${PROJECT_LIBATAPP_ROOT_INC_DIR}/atframe/atapp_conf.proto"
  WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
  COMMENT
    "Generate ${PROJECT_LIBATAPP_ROOT_INC_DIR}/atframe/atapp_conf.pb.cc and ${PROJECT_LIBATAPP_ROOT_SRC_DIR}/atframe/atapp_conf.pb.cc"
)
add_library("${PROJECT_LIBATAPP_LIB_LINK}-protocol" STATIC ${PROJECT_LIBATAPP_PROTOCOL_SRC_LIST})
set_target_properties(
  "${PROJECT_LIBATAPP_LIB_LINK}-protocol"
  PROPERTIES C_VISIBILITY_PRESET "default"
             CXX_VISIBILITY_PRESET "default"
             VERSION ${LIBATAPP_VERSION}
             WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
target_link_libraries(
  "${PROJECT_LIBATAPP_LIB_LINK}-protocol"
  PUBLIC "atbus-protocol"
  INTERFACE ${3RD_PARTY_PROTOBUF_LINK_NAME})
target_include_directories(
  "${PROJECT_LIBATAPP_LIB_LINK}-protocol"
  PRIVATE "$<BUILD_INTERFACE:${PROJECT_LIBATAPP_ROOT_INC_DIR};${3RD_PARTY_PROTOBUF_INC_DIR}>"
  PUBLIC "$<BUILD_INTERFACE:${LIBATBUS_ROOT}/include>"
  # INTERFACE "$<BUILD_INTERFACE:>"
)
if(PROJECT_LIBATAPP_PRIVATE_COMPILE_OPTIONS)
  target_compile_options("${PROJECT_LIBATAPP_LIB_LINK}-protocol"
                         PRIVATE ${PROJECT_LIBATAPP_PRIVATE_COMPILE_OPTIONS})
endif()
if(PROJECT_BUILD_TOOLS_PATCH_PROTOBUF_SOURCES_OPTIONS)
  target_compile_options("${PROJECT_LIBATAPP_LIB_LINK}-protocol"
                         PRIVATE ${PROJECT_BUILD_TOOLS_PATCH_PROTOBUF_SOURCES_OPTIONS})
endif()

if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.15")
  list(PREPEND PROJECT_LIBATAPP_PUBLIC_LINK_NAMES "${PROJECT_LIBATAPP_LIB_LINK}-protocol")
else()
  set(PROJECT_LIBATAPP_PUBLIC_LINK_NAMES "${PROJECT_LIBATAPP_LIB_LINK}-protocol"
                                         ${PROJECT_LIBATAPP_PUBLIC_LINK_NAMES})
endif()

if(BUILD_SHARED_LIBS OR ATFRAMEWORK_USE_DYNAMIC_LIBRARY)
  add_library(${PROJECT_LIBATAPP_LIB_LINK} SHARED ${PROJECT_LIBATAPP_SRC_LIST})
  target_compile_definitions(${PROJECT_LIBATAPP_LIB_LINK} PRIVATE LIBATAPP_MACRO_API_NATIVE=1
                                                                  LIBATAPP_MACRO_API_DLL=1)
  set_target_properties(
    ${PROJECT_LIBATAPP_LIB_LINK}
    PROPERTIES VERSION ${LIBATAPP_VERSION}
               SOVERSION ${LIBATAPP_VERSION}
               INTERFACE_COMPILE_DEFINITIONS LIBATAPP_MACRO_API_DLL=1)
else()
  add_library(${PROJECT_LIBATAPP_LIB_LINK} STATIC ${PROJECT_LIBATAPP_SRC_LIST})
  target_compile_definitions(${PROJECT_LIBATAPP_LIB_LINK} PRIVATE LIBATAPP_MACRO_API_NATIVE=1)
  set_target_properties(${PROJECT_LIBATAPP_LIB_LINK} PROPERTIES VERSION ${LIBATAPP_VERSION})
endif()

set_target_properties(${PROJECT_LIBATAPP_LIB_LINK} PROPERTIES C_VISIBILITY_PRESET "hidden"
                                                              CXX_VISIBILITY_PRESET "hidden")

target_link_libraries(
  ${PROJECT_LIBATAPP_LIB_LINK}
  PUBLIC ${PROJECT_LIBATAPP_PUBLIC_LINK_NAMES}
  INTERFACE ${PROJECT_LIBATAPP_INTERFACE_LINK_NAMES})

target_include_directories(
  ${PROJECT_LIBATAPP_LIB_LINK}
  PUBLIC "$<BUILD_INTERFACE:${PROJECT_LIBATAPP_PUBLIC_INCLUDE_DIRS};${3RD_PARTY_RAPIDJSON_INC_DIR}>"
         "$<INSTALL_INTERFACE:include>"
  PRIVATE "$<BUILD_INTERFACE:${PROJECT_LIBATAPP_ROOT_SRC_DIR}>")

if(PROJECT_LIBATAPP_PRIVATE_COMPILE_OPTIONS)
  target_compile_options(${PROJECT_LIBATAPP_LIB_LINK}
                         PRIVATE ${PROJECT_LIBATAPP_PRIVATE_COMPILE_OPTIONS})
endif()

if(MSVC)
  set_property(TARGET ${PROJECT_LIBATAPP_LIB_LINK} PROPERTY FOLDER "atframework")
  set_property(TARGET "${PROJECT_LIBATAPP_LIB_LINK}-protocol" PROPERTY FOLDER "atframework")
endif()

install(
  TARGETS ${PROJECT_LIBATAPP_LIB_LINK} "${PROJECT_LIBATAPP_LIB_LINK}-protocol"
  EXPORT ${PROJECT_LIBATAPP_EXPORT_NAME}
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

install(
  DIRECTORY ${PROJECT_LIBATAPP_ROOT_INC_DIR}
  DESTINATION .
  FILES_MATCHING
  REGEX ".+\\.h(pp)?$"
  PATTERN ".svn" EXCLUDE
  PATTERN ".git" EXCLUDE)
